<template>
  <div class="login">
    <div class="login_item">
      <el-form ref="elForm" :model="pojo" :rules="rules" size="medium" label-width="100px" label-position="top">
        <el-form-item prop="name">
          <el-input v-model="pojo.name" placeholder="请输入账号" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="pojo.password" placeholder="请输入密码" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>

        <el-form-item size="large">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="warning" @click="chong">忘记密码</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
    import userApi from '@/api/login'
    import {
        setUser
    } from '@/utils/auth'
    export default {

        components: {},
        props: [],
        data() {
            return {
                pojo: {
                    name: undefined,
                    password: undefined,
                },
                rules: {
                    name: [{
                        required: true,
                        message: '请输入账号',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }],
                },
            }
        },
        computed: {},
        watch: {},
        created() {},
        mounted() {},
        methods: {
            submitForm() {
                this.$refs['elForm'].validate(valid => {
                    if (!valid) return
                        // TODO 提交表单
                    this.login()

                })
            },
            resetForm() {
                this.$refs['elForm'].resetFields()
            },
            chong() {
                console.log("你好");
            },
            login() {
                userApi.login(this.pojo.name, this.pojo.password).then(res => {
                    if (res.data.code == 20000) {
                        setUser(res.data.data.token, res.data.data.name, res.data.data.avatar)
                        this.$router.push('manage')
                    } else {
                        this.$message({
                            message: '用户名或密码错误',
                            type: 'error'
                        })
                        this.username = ''
                        this.password = ''
                    }
                })
            }
        }
    }
</script>
<style>
    .login {
        width: 1200px;
        margin: 0 auto;
        background-color: skyblue;
        height: 500px;
        background: url(../assets/images/hu.jpg);
    }

    .login_item {
        width: 800px;
    }

    .el-form {
        transform: translate(28%, 45%);
    }
</style>
